app.controller("projectPlanSaveCtrl", ["$scope", "$rootScope", "$modalInstance", "$http", "$filter", "lodash", "reqDataList", "statusList","$timeout", function ($scope, $rootScope, $modalInstance, $http, $filter, lodash, reqDataList, statusList,$timeout) {
    $scope.info = {};
    $scope.info.ProjectPlanNo = "";
    $scope.projectPlan = {};
    $scope.reqIds = [];
    $scope.info.reqList = [];
    $scope.lock = false;
    $scope.info.charger = {"account": getUserAccount(), "realname": getUserName(), "role": "pm"}


    //table
    $scope.info.reqThdata = [
        {"thName": "产品设计"},
        {"thName": "计划日期"},
        {"thName": "立项"},
        {"thName": "PDM负责人"},
        {"thName": "项目"},
        // {"thName": "计划上线日"}
    ];
    $scope.info.reqPage = {"isTrue": false};
    $scope.info.selectReqList = {
        "trIsClick": false,
        "isOperation": {"Delete": {"isTrue": true, "msg": "移除"}},
        "tdName": [{"name": "reqName"}, {"name": "reqCreateTime"}, {"name": "reqStatusShow"}, {"name": "reqChargerRealname"}, {"name": "projectPlanName"}],
        "datas": []
    };

    $http.get(matrix_product_url + "/projectPlan/getProjectPlanNo").success(function (data) {
        if (data.code == 0) {
            $scope.info.ProjectPlanNo = data.data;
        } else {
            $rootScope.showMessage("error", "失败", "生成项目编码失败");
        }
    })

    $scope.testCode = function () {
        var testStr = /^([a-z]|[A-Z])+([a-z]|[A-Z]|\d|_|)*$/;
        var code = $scope.projectPlan.projectPlanCode;
        if (!code) {
            return;
        }
        if (!code.match(testStr)) {
            $rootScope.toasterWarningInfo("请输入正确的项目代号！");
            $scope.lock = false;
        } else {
            $scope.lock = true;
        }
    };

    $http.get(matrix_product_url + "/brdRequirement/getBrdRequirements?withFeatureDesc=0").success(function (data) {
        if (data.code == 0) {

            angular.forEach(data.data, function (req) {
                if (req.reqStatus == 'NEW' || req.reqStatus == 'DISSOCIATION') {
                    req.crtname = req.reqName;
                    req.selectTrue = false;
                    req.reqStatusShow = lodash.findWhere(statusList, {"key": req.reqStatus}) ? lodash.findWhere(statusList, {"key": req.reqStatus}).value : req.reqStatus
                    $scope.info.reqList.push(req)
                }
            })
            if (reqDataList != null) {
                $timeout(function () {
                    angular.forEach(reqDataList,function (selectReq) {
                        selectReq.reqCreateTime = timestampToTime(selectReq.reqCreateTime);
                        $scope.reqIds.push(selectReq.reqId)
                        selectReq.reqStatusShow = lodash.findWhere(statusList, {"key": selectReq.reqStatus}) ? lodash.findWhere(statusList, {"key": selectReq.reqStatus}).value : selectReq.reqStatus
                        lodash.remove($scope.info.reqList, {reqId: selectReq.reqId});
                        $scope.info.selectReqList.datas.push(selectReq);
                    })
                })
            }
        } else {
            $rootScope.showMessage("error", "失败", data.msg);
            $scope.$apply();
        }
    })
    //添加req
    $scope.addReqs = function () {
        if ($scope.info.req != undefined && $scope.info.req != "" && !$scope.info.req.selectTrue) {
            $scope.info.req.reqCreateTime = timestampToTime($scope.info.req.reqCreateTime);
            $scope.info.selectReqList.datas.push($scope.info.req);
            $scope.reqIds.push($scope.info.req.reqId)
            $scope.info.req.selectTrue = true;
            $scope.info.req = "";
        }

    }
    //移除req
    $scope.deleteReq = function (info) {
        lodash.remove($scope.info.selectReqList.datas, {reqId: info.reqId});
        angular.forEach($scope.info.reqList, function (req) {
            if (req.reqId == info.reqId) {
                req.selectTrue = false;
            }
        })
    }

    $scope.createClick = function () {
        if (!$scope.projectPlan.projectPlanName) {
            $("#projectPlan-projectPlanName").focus();
            $rootScope.toasterWarningInfo("请输入标题");
            return false;
        }
        if (!$scope.projectPlan.projectPlanCode) {
            $("#projectPlan-projectPlanCode").focus();
            $rootScope.toasterWarningInfo("请输入正确的项目代号！");
            return false;
        }
        if (!$scope.lock) {
            $("#projectPlan-projectPlanCode").focus();
            $rootScope.toasterWarningInfo("请输入正确编码!");
            return false;
        }
        if ($scope.info.pdm == undefined) {
            $("#projectPlan-projectPlanCode").focus();
            $rootScope.toasterWarningInfo("请选择pdm负责人!");
            return false;
        }
        if ($scope.info.charger == undefined) {
            $("#projectPlan-projectPlanCode").focus();
            $rootScope.toasterWarningInfo("请选择项目负责人!");
            return false;
        }
        $scope.projectPlan.projectPlanPdmAccount = $scope.info.pdm.account;
        $scope.projectPlan.projectPlanPdmRealname = $scope.info.pdm.realname;
        $scope.projectPlan.projectPlanChargerAccount = $scope.info.charger.account;
        $scope.projectPlan.projectPlanChargerRealname = $scope.info.charger.realname;
        $scope.projectPlan.projectPlanNo = $scope.info.ProjectPlanNo;
        $scope.projectPlan.projectPlanCreaterAccount = getUserAccount();
        $scope.projectPlan.projectPlanCreaterRealname = getUserName();
        $scope.projectPlan.tokenId = getTokenId();
        $scope.projectPlan.reqIds = $scope.reqIds;
        $scope.projectPlan.memebers = JSON.stringify($scope.setRoleArray());
        $.ajax({
            type: 'post',
            url: matrix_product_url + '/projectPlan/saveProjectPlan',
            data: JSON.stringify($scope.projectPlan),
            contentType: "application/json; charset=utf-8",
            success: function (data) {
                if (data.code == '0') {
                    $scope.cancelClick('save', $scope.reqIds);
                    $rootScope.toasterSuccessInfo("操作成功！");
                } else {
                    $rootScope.showMessage("error", "失败", data.desc)
                    $scope.$apply();
                }
            }
        })
    };

    $scope.cancelClick = function (x, reqIds) {
        if (x == 'save') {
            $modalInstance.close(reqIds);
        }
        if (x == 'cancel') {
            $modalInstance.close(false);
        }
    };

    $scope.setRoleArray = function () {
        var roleArray = [];
        if ($scope.info.charger.account == $scope.info.pdm.account) {
            $scope.info.charger.role = "pm"
            roleArray.push({
                "account": $scope.info.charger.account,
                "realname": $scope.info.charger.realname,
                "role": $scope.info.charger.role
            });
        }else {
            $scope.info.charger.role = "pm"
            $scope.info.pdm.role = "pdm"
            roleArray.push({
                "account": $scope.info.charger.account,
                "realname": $scope.info.charger.realname,
                "role": $scope.info.charger.role
            });
            roleArray.push({
                "account": $scope.info.pdm.account,
                "realname": $scope.info.pdm.realname,
                "role": $scope.info.pdm.role
            });
        }
        return roleArray;
    }
}]);
